package com.test.test01.rabbitmq.sender.impl;

import com.test.test01.rabbitmq.sender.RabbitMQService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Description: RabbitMQ service接口实现类
 * Create DateTime: 2020/10/26 9:31
 *
 * @author huangzi
 */
@Slf4j
@Service
public class RabbitMQServiceImpl implements RabbitMQService {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public void sendSimpleMessage(String exchange, String routingKey, Object data) {
        rabbitTemplate.convertAndSend(exchange, routingKey, data);
    }

    @Override
    public void sendDelayMessage(String exchange, String routingKey, Object data, Integer delayTime) {
        rabbitTemplate.convertAndSend(exchange, routingKey, data, message -> {
            //发送方式--延迟队列--交换机
            message.getMessageProperties().setDelay(delayTime * 1000);
            //发送方式--延迟队列--队列
            //message.getMessageProperties().setExpiration(delayTime * 1000 + "");
            return message;
        });
    }
}
